// src/main/java/com/bookmanager/controller/FavoriteController.java
package com.bookmanager.controller;

import com.bookmanager.model.Favorite;
import com.bookmanager.model.User;
import com.bookmanager.service.FavoriteService;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/favorite")
public class FavoriteController extends HttpServlet {
    private FavoriteService favoriteService = new FavoriteService();

    // src/main/java/com/bookmanager/controller/FavoriteController.java

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user == null) {
            response.sendRedirect("login.jsp");
            return;
        }

        // 显示收藏列表
        List<Favorite> favorites = favoriteService.getUserFavorites(user.getUserId());

        // 确保设置属性
        request.setAttribute("favorites", favorites);

        // 使用转发而不是重定向
        RequestDispatcher dispatcher = request.getRequestDispatcher("favorites.jsp");
        dispatcher.forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user == null) {
            response.sendRedirect("login.jsp");
            return;
        }

        String action = request.getParameter("action");
        int bookId = Integer.parseInt(request.getParameter("bookId"));

        try {
            boolean success = false;
            String message = "";

            if ("add".equals(action)) {
                // 先检查是否已经收藏
                if (favoriteService.isBookFavorite(user.getUserId(), bookId)) {
                    message = "您已经收藏过这本书了！";
                } else {
                    success = favoriteService.addFavorite(user.getUserId(), bookId);
                    message = success ? "收藏成功！" : "收藏失败，请重试！";
                }
            } else if ("remove".equals(action)) {
                success = favoriteService.removeFavorite(user.getUserId(), bookId);
                message = success ? "取消收藏成功！" : "取消收藏失败，请重试！";
            }

            // 设置成功消息
            session.setAttribute("message", message);

            String referer = request.getHeader("Referer");
            if (referer != null) {
                // 如果是从用户中心来的，重定向回用户中心
                if (referer.contains("dashboard")) {
                    response.sendRedirect(referer);
                } else {
                    response.sendRedirect(referer);
                }
            } else {
                response.sendRedirect("books");
            }

        } catch (NumberFormatException e) {
            session.setAttribute("message", "无效的图书ID");
            response.sendRedirect("books");
        }
    }
}